package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ILogIntegralService;
import com.mdd.admin.validate.LogIntegralCreateValidate;
import com.mdd.admin.validate.LogIntegralUpdateValidate;
import com.mdd.admin.validate.LogIntegralSearchValidate;
import com.mdd.admin.vo.LogIntegralListedVo;
import com.mdd.admin.vo.LogIntegralDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.LogIntegral;
import com.mdd.common.mapper.LogIntegralMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 积分变动实现类
 * @author DEREK
 */
@Service
public class LogIntegralServiceImpl implements ILogIntegralService {
        
    @Resource
    LogIntegralMapper logIntegralMapper;

    /**
     * 积分变动列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<LogIntegralListedVo>
     */
    @Override
    public PageResult<LogIntegralListedVo> list(PageValidate pageValidate, LogIntegralSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<LogIntegral> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        logIntegralMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:sn:str",
            "=:userId@user_id:int",
            "=:sourceId@source_id:int",
            "=:sourceSn@source_sn:str",
            "=:changeType@change_type:int",
            "=:changeAmount@change_amount:str",
            "=:leftAmount@left_amount:str",
            "=:action:int",
            "=:remark:str",
            "=:extra:str",
        });

        IPage<LogIntegral> iPage = logIntegralMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<LogIntegralListedVo> list = new LinkedList<>();
        for(LogIntegral item : iPage.getRecords()) {
            LogIntegralListedVo vo = new LogIntegralListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 积分变动详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return LogIntegral
     */
    @Override
    public LogIntegralDetailVo detail(Integer id) {
        LogIntegral model = logIntegralMapper.selectOne(
                new QueryWrapper<LogIntegral>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        LogIntegralDetailVo vo = new LogIntegralDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 积分变动新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(LogIntegralCreateValidate createValidate) {
        LogIntegral model = new LogIntegral();
        model.setSn(createValidate.getSn());
        model.setUserId(createValidate.getUserId());
        model.setSourceId(createValidate.getSourceId());
        model.setSourceSn(createValidate.getSourceSn());
        model.setChangeType(createValidate.getChangeType());
        model.setChangeAmount(createValidate.getChangeAmount());
        model.setLeftAmount(createValidate.getLeftAmount());
        model.setAction(createValidate.getAction());
        model.setRemark(createValidate.getRemark());
        model.setExtra(createValidate.getExtra());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        logIntegralMapper.insert(model);
    }

    /**
     * 积分变动编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(LogIntegralUpdateValidate updateValidate) {
        LogIntegral model = logIntegralMapper.selectOne(
                new QueryWrapper<LogIntegral>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setSn(updateValidate.getSn());
        model.setUserId(updateValidate.getUserId());
        model.setSourceId(updateValidate.getSourceId());
        model.setSourceSn(updateValidate.getSourceSn());
        model.setChangeType(updateValidate.getChangeType());
        model.setChangeAmount(updateValidate.getChangeAmount());
        model.setLeftAmount(updateValidate.getLeftAmount());
        model.setAction(updateValidate.getAction());
        model.setRemark(updateValidate.getRemark());
        model.setExtra(updateValidate.getExtra());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        logIntegralMapper.updateById(model);
    }

    /**
     * 积分变动删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        LogIntegral model = logIntegralMapper.selectOne(
                new QueryWrapper<LogIntegral>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        logIntegralMapper.delete(new QueryWrapper<LogIntegral>().eq("id", id));
    }

}
